package com.lxyk.service.statistic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.dao.statistic.ClassAttendanceDao;
import com.lxyk.dao.statistic.StuTaskDao;
import com.lxyk.dao.user.ClassesDao;
import com.lxyk.domain.ClassAttendance;
import com.lxyk.domain.Classes;
import com.lxyk.domain.StuStatisticVO;
import com.lxyk.service.statistic.StuTaskService;
import com.lxyk.utils.StatisticUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//学生作业率
@Service
public class StuTaskServiceImpl implements StuTaskService {
    @Autowired
    private StuTaskDao stuTaskDao;
    @Autowired
    private ClassAttendanceDao classAttendanceDao;
    @Autowired
    private ClassesDao classesDao;
    //查询班级某一天所有学生作业率数据
    @Override
    public List<StuStatisticVO> findClassDayTask(Integer cid, Date recordDate) {
        List<StuStatisticVO> data = stuTaskDao.findClassDayTask(cid, recordDate);
        return data;
    }

    //分页查询:班级某一天所有学生作业率数据
    @Override
    public PageInfo<StuStatisticVO> findClassDayTask(Integer cid, Date recordDate, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        Integer[] yearAndMonth = StatisticUtils.getYearAndMonth(date);
        List<StuStatisticVO> data = stuTaskDao.findClassDayTask(cid, recordDate);
        PageInfo<StuStatisticVO> pageInfo = new PageInfo<>(data);
        return pageInfo;
    }

    //条件查询
    @Override
    public PageInfo<StuStatisticVO> findByCondition(StuStatisticVO stuStatisticVO, Integer pageNum, Integer pageSize) {
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        PageHelper.startPage(pageNum,pageSize);
        List<StuStatisticVO> data = stuTaskDao.findByCondition(stuStatisticVO);
        PageInfo<StuStatisticVO> pageInfo = new PageInfo<>(data);
        return pageInfo;
    }

    //更新作业率(批量)
    @Override
    public void save(Integer cid, Date recordDate, List<StuStatisticVO> list) {
        if(list.get(0).getId()!=null){
            //修改当前班级学生作业率
            stuTaskDao.update(list);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            if(!sdf.format(oldDate).equals(sdf.format(recordDate))){
//                //删除原来数据
//                classAttendanceDao.del(cid,oldDate);
//            }
        }else {
            stuTaskDao.add(list);
        }
        //修改统计数据
        //1 重新计算当前班级,当前年/月/日的作业率
        //查询班级交作业人数
        int realCount = stuTaskDao.findRealTaskCount(cid, recordDate);
//        int allCount = stuTaskDao.findAllTaskCount(cid,recordDate);
        //班级有效人数
        Classes classes = classesDao.findById(cid);
        Integer allCount = classes.getEffeNumber();//班级有效人数
        double avgTask = Double.valueOf(realCount)/allCount;
        avgTask = Double.valueOf(String.format("%.2f",avgTask))*100;
        System.out.println("实际交作业人数:"+realCount);
        System.out.println("应交作业人数:"+allCount);
        System.out.println("平均作业率是:"+avgTask);
        //查询当前班级日期是否有统计数据
        ClassAttendance classAttendance = classAttendanceDao.findByClassAndDate(cid, recordDate);
        if(classAttendance!=null){
            //2 修改
            classAttendanceDao.updateTask(cid,recordDate,avgTask);
        }else{
            //3 添加
            classAttendanceDao.addTask(cid,recordDate,avgTask);
        }

    }

    //根据班级id和日期查询学生信息
    @Override
    public PageInfo<StuStatisticVO> findStudentByCidAndDate(Integer cid, Date recordDate, Integer pageNum, Integer pageSize) {
        //只查询没有添加到课率的学生
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        PageHelper.startPage(pageNum,pageSize);
        List<StuStatisticVO> list = stuTaskDao.findStudentByCidAndDate(cid, recordDate);
        return new PageInfo<>(list);
    }

    @Override
    public Date getLastDate(Integer cid) {
        return stuTaskDao.getLastDate(cid);
    }


}
